<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Upload extends CI_Controller
{
	public function __construct()
	{
        parent::__construct();
		$this->pre_message = "";
        $this->load->model('upload_model','upload');
	}
	
	function index($start=0)
	{
		$data['title'] = 'Khách hàng liên hệ';
		//$data['add'] = 'upload/add';
		$config['base_url'] 	= base_url().'upload/index/'.(int)$start;  
		$data['num'] 			= $this->upload->count_list();
		$config['total_rows'] 	= $data['num'];
		$config['per_page'] 	= 20;
		$config['uri_segment'] 	= 4;   
		$this->pagination->initialize($config);   
		$data['pagination'] 	= $this->pagination->create_links();  
		      
		$data['start'] = $start ;    
		$data['list'] =   $this->upload->get_list($config['per_page'],(int)$start);
		$data['page'] = 'upload/index';
		$this->load->view('layout/skin',$data);
	}
	
	function view($id=0)
	{            
		$data['rs'] = $this->upload->getlienhe($id);
		$data['title'] = 'Xem thông tin liên hệ upload từ '.$data['rs']->lb_cusmail;
		
		//Cập nhật trang thái
		$this->upload->set_view($id);
		 
		$data['page'] = 'upload/view';
		$this->load->view('layout/skin',$data);
	}
	
	/*function add()
	{
	  $data['title'] = 'Thêm upload mới';
	
	  $this->form_validation->set_rules('subject_vn','Tiêu đề','trim|required');
	  $this->form_validation->set_rules('ks_image','','');
	  $this->form_validation->set_rules('subcontent_vn','','');
	  $this->form_validation->set_rules('content_vn','','');
	  if($this->form_validation->run()== FALSE){
		  $this->pre_message = validation_errors();
	  }else{
		  if($this->upload->save()){
			 if($this->input->post('ks_image')!=''){
				 if(file_exists('../uploads/images/'.$this->input->post('ks_image'))){
					 unlink('../uploads/images/'.$this->input->post('ks_image'));
				 }
				//fn_resize_ks_image('../uploads/images/'.$this->input->post('ks_image'),'../uploads/upload/'.$this->input->post('ks_image'),160,120); 
			  }                      
			  $this->session->set_flashdata('message','Lưu thành công');
			  redirect('upload/index');
		  }
	  }
	
	  $data['message'] = $this->pre_message;
	  $this->_templates['page'] = 'upload/add';
	  $this->site->load($this->_templates['page'],$data);
	}*/

	function edit()
	{            
		$id = (int)$this->uri->segment(3);                              
		$data['rs'] = $this->upload->getlienhe($id);
		$data['title'] = 'Cập nhật: '.$data['rs']->subject_vn;
		
		if(!empty($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('subject_vn','Tên chủ đề','trim|required');
		
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}else{
				if($this->upload->save())
				{
					$this->session->set_flashdata('message','Cập nhật chuyên mục thành công');
					redirect('upload');
				}
		 	}
		}  
		$this->_templates['page'] = 'upload/edit';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function del($id=0,$start=0)
	{
		$this->upload->delete($id);
		$this->upload->remove();
		redirect("upload/index/".$start);
	}
	
	function dels()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
		
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->upload->deleted($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('upload/upload/index/'.$page);
	}
}

?>